import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/controllers/sports/movement/logic.dart';

class TimerDialog extends Dialog {

  final logic = Get.put(MovementLogic());
  late Timer _timer;
  int _countDown = 3;
  Function callback;

  TimerDialog({super.key, required this.callback});

  @override
  Widget build(BuildContext context) {
    logic.state.timerCount = '3';
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      _countDown--;
      if(_countDown<0) {
        _timer.cancel();
        Navigator.pop(context);
        callback(_countDown);
        return;
      }

      if(_countDown==0) {
        logic.timerDown('GO');
      } else {
        logic.timerDown('$_countDown');
      }
    });
    return GestureDetector( // 点击遮罩层隐藏弹框
        child: Material(
            color: Colors.black45,
            // type: MaterialType.transparency, // 配置透明度
            child: Center(
              child: GetBuilder<MovementLogic>(
                id: 'timer_down',
                builder: (_) {
                  return Text(logic.state.timerCount, style: TextStyle(color: Colors.white, fontSize: 700.sp),);
                },
              ),
            ),
        ),
        onTap: () {
          Navigator.pop(context);
        }
    );
  }
}